package com.cndw.kongfu;

import java.io.FileInputStream;
import java.io.InputStream;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;

public class ExcelDataProcesser {

	public static final int DIRECTION_ROW = 1;
	public static final int DIRECTION_COLUMN = 2;
	public static final int ROWINDWX = 24;
	public static final int COLUMNINDEX = 7;
	public static final String PATH = "C:/Users/Administrator/Desktop/《武道破天》部分服花灯许愿积分前10名.xls";

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println(getFormatData(PATH, ROWINDWX, COLUMNINDEX, DIRECTION_ROW));
	}

	public static String getFormatData(String path, int rowIndex, int columnIndex, int direction) {
		StringBuffer value = new StringBuffer();
		Workbook wookbook = null;
		InputStream is = null;
		try {
			// 创建对Excel工作簿文件的引用­

			is = new FileInputStream(path);
			wookbook = new HSSFWorkbook(is);

			HSSFSheet sheet = (HSSFSheet) wookbook.getSheetAt(0);

			// 获取到Excel文件中的所有行数
			int rows = sheet.getPhysicalNumberOfRows();
			// 遍历行
			value.append("{");
			for (int i = rowIndex; i < rows; i++) {
				HSSFRow row = sheet.getRow(i);

				if (row != null) {

					// 获取到列的值
					HSSFCell cell = row.getCell(columnIndex);

					if (cell != null) {
						switch (cell.getCellType()) {
						case HSSFCell.CELL_TYPE_FORMULA:
							break;
						case HSSFCell.CELL_TYPE_NUMERIC:
							value.append((int)(cell.getNumericCellValue()));
							value.append(", ");
							break;
						case HSSFCell.CELL_TYPE_STRING:
							value.append(cell.getStringCellValue().trim());
							value.append(", ");
							break;
						case HSSFCell.CELL_TYPE_BLANK:
							// valueClause.append("'' ");
							break;
						default:

							break;
						}
					}
				}
			}
			value.append("}");
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return value.toString();
	}
	public static String getFormatData2(String path, int rowIndex,
			int columnIndex, int direction) {
		StringBuffer value = new StringBuffer();
		Workbook wookbook = null;
		InputStream is = null;
		try {
			// 创建对Excel工作簿文件的引用­
			
			is = new FileInputStream(path);
			wookbook = new HSSFWorkbook(is);
			
			HSSFSheet sheet = (HSSFSheet) wookbook.getSheetAt(0);
			
			// 获取到Excel文件中的所有行数
			int rows = sheet.getPhysicalNumberOfRows();
			// 遍历行
			value.append("{");
			for (int i = rowIndex; i < rows; i++) {
				HSSFRow row = sheet.getRow(i);
				
				if (row != null) {
					
					// 获取到列的值
					HSSFCell cell = row.getCell(columnIndex);
					
					if (cell != null) {
						switch (cell.getCellType()) {
						case HSSFCell.CELL_TYPE_FORMULA:
							break;
						case HSSFCell.CELL_TYPE_NUMERIC:
							value.append("{");
							int v = (int)(cell.getNumericCellValue());
							value.append(v / 10);
							value.append(", ");
							value.append(v % 10);
							value.append("}");
							value.append(", ");
							break;
						case HSSFCell.CELL_TYPE_STRING:
							value.append(cell.getStringCellValue().trim());
							value.append(", ");
							break;
						case HSSFCell.CELL_TYPE_BLANK:
							// valueClause.append("'' ");
							break;
						default:
							
							break;
						}
					}
				}
			}
			value.append("}");
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return value.toString();
	}

}
